# -- coding: utf-8 --
from tools.Request_General import BaseAPI
from tools.ReadJson import JsonParse


class DataSource(BaseAPI):
    """
    数据源相关接口
    """

    def addShareDs(self, payload, get_ids=False):
        """新增共享数据源"""
        r = self.request_send(json=payload)
        if get_ids:
            share_res = self.searchShareDb("0").json()
            # 获取新增数据源的id
            share_ids = [JsonParse(share_res).query_same_level_value("$..aliasName", share.get("aliasName"), "id") for share
                         in payload]
            return share_ids
        return r

    def delDsByIdList(self, payload):
        """根据id删除数据源"""
        r = self.request_send(json=payload)
        return r

    def searchShareDb(self, uri):
        """查询数据源"""
        r = self.request_send(uri=str(uri))
        return r


if __name__ == '__main__':
    from application.general_create import COOKIE, APP_ID, SPACE_ID

    headers = {"cookie": COOKIE, "language": "zh-cn", "app": APP_ID, "space": SPACE_ID}
    r = DataSource(headers=headers).addShareDs([
    {
      "aliasName": "v3_test",
      "url": "192.168.6.11",
      "port": "3306",
      "dataBaseName": "test_deep_pipeline",
      "userName": "root",
      "passWord": "Linux123!@#",
      "insUuid": "31d22419d34542f58023b56b6bcc3827",
      "jdbcUrl": "jdbc:mysql://192.168.6.11:3306/test_deep_pipeline?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true",
      "shareDb": 0,
      "driverId": 1,
      "jdbcTemplate": "jdbc:mysql://{host}:{port}/{database}?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true",
      "driver": "com.mysql.cj.jdbc.Driver",
      "dbType": "MYSQL",
      "driverUuid": "06742f96-5b4e-11ed-baa6-00163e2c2ed8",
      "dbVersion": "5.7.30-log"
    },
    {
      "aliasName": "sh_test_ck",
      "url": "cc-2ze34m58grmq0vr5j.ads.rds.aliyuncs.com",
      "port": "8123",
      "dataBaseName": "wth",
      "userName": "admin",
      "passWord": "Y942soX#@3cSWX",
      "insUuid": "a2d3a505ba6e464290ff48323c3aba8a",
      "jdbcUrl": "jdbc:clickhouse://cc-2ze34m58grmq0vr5j.ads.rds.aliyuncs.com:8123/wth",
      "shareDb": 0,
      "driverId": 6,
      "jdbcTemplate": "jdbc:clickhouse://{host}:{port}/{database}",
      "driver": "com.clickhouse.jdbc.ClickHouseDriver",
      "dbType": "CLICKHOUSE",
      "driverUuid": "068c2532-5b4e-11ed-baa6-00163e2c2ed8",
      "dbVersion": "20.8.17.25"
    }
  ])
    import pprint

    pprint.pprint(r)
